package com.sky.module.system.loginlog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.module.system.utils.IpUtil;
import com.sky.module.system.loginlog.dao.LoginLogMapper;
import com.sky.module.system.loginlog.domain.entity.LoginLog;
import com.sky.module.system.loginlog.domain.form.LoginLogFormQuery;
import com.sky.module.system.loginlog.service.LoginLogService;
import com.sky.module.system.user.domain.entity.User;
import com.sky.module.system.utils.RequestInfoUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @date 2023/3/8 20:56
 **/
@Service
public class LoginLogServiceImpl implements LoginLogService {
    @Autowired
    private LoginLogMapper loginLogMapper;

    @Override
    public Page<LoginLog> queryPage(LoginLogFormQuery loginLogFormQuery) {
        Page<LoginLog> page = new Page<>(loginLogFormQuery.getPageNum(), loginLogFormQuery.getPageSize());
        LambdaQueryWrapper<LoginLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(loginLogFormQuery.getUsername()), LoginLog::getUsername, loginLogFormQuery.getUsername());
        wrapper.like(StringUtils.isNotBlank(loginLogFormQuery.getIpaddr()), LoginLog::getIpaddr, loginLogFormQuery.getIpaddr());
        // 大于等于 开始时间
        wrapper.ge(StringUtils.isNotBlank(loginLogFormQuery.getBeginTime()), LoginLog::getCreateTime, loginLogFormQuery.getBeginTime());

//         小于等于
        wrapper.le(StringUtils.isNotBlank(loginLogFormQuery.getEndTime()), LoginLog::getCreateTime, loginLogFormQuery.getEndTime());
        User user = RequestInfoUtils.getUser();
        Page<LoginLog> pageResult = loginLogMapper.selectPage(page, wrapper);
        pageResult.getRecords().stream().forEach(e -> {
            e.setUserId (user.getId());
        });
        return pageResult;
    }

    /***
     * @description TODO
     *
     * @return void
     * @date 2023/3/8 22:06
     *
     */
    @Override
    public void saveLoginLogInfoSuccess() {

        LoginLog loginLog = new LoginLog();
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        loginLog.setAccessTime(new Date());
//        BeanUtils.copyProperties(loginLogDto, loginLog);
        loginLog.setCreateTime(new Date());
        loginLog.setUpdateTime(new Date());
        loginLog.setStatus(1); // 表示登录成功
        loginLog.setUsername(user.getUsername());
        ServletRequestAttributes sa = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sa.getRequest();
        UserAgent userAgent = new UserAgent(request.getHeader("User-Agent"));
        loginLog.setUserAgent(userAgent.toString());
        String ipAddress = IpUtil.getIpAddress(request);
        loginLog.setIpaddr(ipAddress);
        loginLog.setMsg("登录成功");
        loginLogMapper.insert(loginLog);

    }

    @Override
    public void saveLoginLogInfoFail() {
//        / TODO 抽取方法
        // 当前时间
        Date date = new Date();
        LoginLog loginLog = new LoginLog();
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        loginLog.setAccessTime(date);
        loginLog.setCreateTime(date);
        loginLog.setUpdateTime(date);
        loginLog.setStatus(0); // 表示登录失败
        loginLog.setUsername(user.getUsername());
        ServletRequestAttributes sa = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sa.getRequest();
        String ipAddress = IpUtil.getIpAddress(request);
        loginLog.setIpaddr(ipAddress);
        loginLog.setMsg("登录失败");
        loginLogMapper.insert(loginLog);
    }

    @Override
    public boolean deleteById(Integer id) {
        return false;
    }
}
